package fserv.entities;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Represents the "Reply" message of the Chat System communication protocol.
 * 
 * @author Arthur Bit-Monnot <abit@etud.insa-toulouse.fr>
 * @author Julien Marchand <jmarchan@etud.insa-toulouse.fr>
 */
public class Reply extends Message {
    private static final byte MSG_ID = 1;
    
	private String message;
    
    public Reply(String message) {
        this.message = message;
    }
    
    public Reply(InputStream stream) throws InvalidMsgIdException, IOException {
        DataInputStream dis = new DataInputStream(stream);
        byte msgId = dis.readByte();
        if (msgId == MSG_ID) {
            this.message = dis.readUTF();
        } else {
            throw new InvalidMsgIdException(msgId);
        }
    }
    
    public String getMessage() {
        return this.message;
    }
    
    @Override
    public void writeToStream(OutputStream stream) throws IOException {
        DataOutputStream dos = new DataOutputStream(stream);
        dos.writeByte(MSG_ID);
        dos.writeUTF(this.message); 
    }

	@Override
	public String toString() {
		return this.message;
	}
}